package com.example.demo;

import com.example.demo.entity.Dept;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
public class RedisTest001 {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static final ObjectMapper mapper = new ObjectMapper();

    @Test
    void testSaveUser() throws JsonProcessingException {
        // 创建对象
        Dept dept = new Dept("小宋", 21);
        // 手动序列化
        String json = mapper.writeValueAsString(dept);
        System.out.println("json = " + json);
        // 写入数据
        stringRedisTemplate.opsForValue().set("dept:200", json);

        // 获取数据
        String jsonUser = stringRedisTemplate.opsForValue().get("dept:200");
        System.out.println("jsonUser = " + jsonUser);
        // 手动反序列化
        Dept user1 = mapper.readValue(jsonUser, Dept.class);
        System.out.println("user1 = " + user1);

    }

}
